/*********************************************************************\
* This File is a part of BH-PANEL (Breizh-Heberg Panel v2.1)
* Copyright (C) 2008-2009 the BH Developpers : Vincent Giersch <mail@vincordi.fr>, Cyprien Laleau <fanning.fr@gmail.com> and Edwin Cabiten <marmottes44@hotmail.fr>
* See file AUTHORS to get more informations

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
/*********************************************************************/

var useAltForKeyboardNavigation = false; 

function tasklistInit() {
  Caret.init();
}

var Cookie = {
  getVar: function(name) {
    var cookie = document.cookie;
    if (cookie.length > 0) {
      cookie += ';';
    }
    re = new RegExp(name + '\=(.*?);' );
    if (cookie.match(re)) {
      return RegExp.$1;
    } else {
      return '';
    }
  },
  setVar: function(name,value,expire,path) {
    document.cookie = name + '=' + value;
  },
};

var Caret = {
  init: function () {
    var task = Cookie.getVar('current_task') || 'top';
    if (task == 'bottom' || task == 'top') {
      var tab = $('tasklist_table');
      var rows = tab ? tab.getElementsByTagName('tbody')[0].getElementsByTagName('tr') : [];
      Caret.currentRow = (task == 'top' || rows.length == 0) ? rows[0] : rows[rows.length-1];
    }
    else {
      Caret.currentRow = $('task'+task);
    }
    if (Caret.currentRow) {
      Element.addClassName(Caret.currentRow,'current_row');
      Event.observe(document,'keydown',Caret.keypress);
    }
  },
  keypress: function (e) {
    var src = Event.element(e);
    if (/input|select|textarea/.test(src.nodeName.toLowerCase())) {
        // don't do anything if key is pressed in input, select or textarea
      return;
    }
    if ((useAltForKeyboardNavigation && !e.altKey) ||
        (!useAltForKeyboardNavigation && e.altKey) ||
         e.ctrlKey || e.shiftKey) {
      return;
    }
    switch (e.keyCode) {
      case 74:       // user pressed "j" move down
          Element.removeClassName(Caret.currentRow,'current_row');
          Caret.nextRow();
          Element.addClassName(Caret.currentRow,'current_row');
          Event.stop(e);
          break;
      case 75:      // user pressed "k" move up
          Element.removeClassName(Caret.currentRow,'current_row');
          Caret.previousRow();
          Element.addClassName(Caret.currentRow,'current_row');
          Event.stop(e);
          break;
      case 79:     // user pressed "o" open task
          window.location = Caret.currentRow.getElementsByTagName('a')[0].href;
          Event.stop(e);
          break;
    }
  },
  nextRow: function () {
    var row = Caret.currentRow;
    while ((row = row.nextSibling)) {
      if ('tr' == row.nodeName.toLowerCase()) {
        Caret.currentRow = row;
        return;
      }
    }
    // we've reached the bottom of the list
    if ($('next')) {
      Cookie.setVar('current_task','top');
      window.location = $('next').href;
      return;
    }
  },
  previousRow: function () {
    var row = Caret.currentRow;
    while ((row = row.previousSibling)) {
      if ('tr' == row.nodeName.toLowerCase()) {
        Caret.currentRow = row;
        return;
      }
    }
    // we've reached the top of the list
    if ($('previous')) {
      Cookie.setVar('current_task','bottom');
      window.location = $('previous').href;
      return;
    }
    
  }
};


function addEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}



function setUpTasklistTable() {
  if (!$('liste_maint')) {
    // No tasklist on the page
    return;
  }
  var table = $('liste_maint');
  addEvent(table,'click',tasklistTableClick);
}

function eventGetSrc(e) {
  if (e.target) {
    return e.target;
  } else if (window.event) {
    return window.event.srcElement;
  } else {
    return;
  }
}

function tasklistTableClick(e) {
  var src = eventGetSrc(e);
  if (src.nodeName != 'TD') {
    return;
  }
  if (src.hasChildNodes()) {
    var checkBoxes = src.getElementsByTagName('input');
    if (checkBoxes.length > 0) {
      // User clicked the cell where the task select checkbox is
      if (checkBoxes[0].checked) {
        checkBoxes[0].checked = false;
      } else {
        checkBoxes[0].checked = true;
      }
      return;
    }
  }
  
  var row = src.parentNode;
  var aElements = row.getElementsByTagName('a');
  if (aElements.length > 0) {
    window.location = aElements[0].href;
  } else {
    // If both the task id and the task summary columns are non-visible
    // just use the good old way to get to the task
    window.location = '#';
  }
}



	function aff_detail(id,action)
	{
	
		function desc(id_d)
		{
			for (var i = 0; i <= 40; i++)
			{
				setTimeout(function(){ document.getElementById('aff_maint_'+id_d).style.top = i+'%'}, i+'000');
			}
		}
		
		if (action == 'aff')
		{
			document.getElementById('maint_background').style.display = 'block';
			document.getElementById('aff_maint_'+id).style.display = 'block';
			desc(id);				
		}
		else if (action == 'disp')
		{
			document.getElementById('aff_maint_'+id).style.display = 'none';
			document.getElementById('aff_maint_'+id).style.top = 0;
			document.getElementById('maint_background').style.display = 'none';
		}
		else if (action == 'last')
		{
			var new_id = id - 1;
			document.getElementById('aff_maint_'+id).style.display = 'none';
			document.getElementById('aff_maint_'+new_id).style.top = 0;
			
			document.getElementById('aff_maint_'+new_id).style.display = 'block';
			desc(new_id);
		}
		else if (action == 'next')
		{	
			var new_id = id + 1;
			document.getElementById('aff_maint_'+id).style.display = 'none';
			document.getElementById('aff_maint_'+new_id).style.top = 0;
			
			document.getElementById('aff_maint_'+new_id).style.display = 'block';
			desc(new_id);
		}
	}





